package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: charls
 * @Description:TODO
 * @Date: 2022/8/8 11:45
 * @Version: 1.0
 */
@RestController //@Controller,@ResponseBody
@CrossOrigin //解决跨域问题
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 品牌列表查询
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Brand> brandList=brandService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",brandList);
    }

    /**
     * 根据ID查询品牌数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Brand brand=brandService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",brand);
    }

    /**
     * 添加品牌
     * @param brand
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 修改品牌数据
     * @param brand
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Brand brand,@PathVariable("id") Integer id){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delById(@PathVariable("id") Integer id){
        brandService.delById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 多条件查询
     * @param searchMap
     * @return
     */
    @GetMapping("/search") // 括号中的value可以不加
    public Result findList(@RequestParam Map searchMap){
        List<Brand> list = brandService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    /**
     * 分页查询实现
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page")int page,@PathVariable("size")int size){
        Page<Brand> pageList = brandService.findPage(page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 多条件加分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/searchListPage/{page}/{size}" )
    public Result findListPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
        Page<Brand> pageList = brandService.findListPage(searchMap, page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 根据分类名称查询品牌列表
     * @param categoryName
     * @return
     */
    @GetMapping("/category/{categoryName}")
    public Result findListByCategoryName(@PathVariable("categoryName") String categoryName){
        List<Map> brandList = brandService.findListByCategoryName(categoryName);
        return new Result(true,StatusCode.OK,"查询成功",brandList);
    }
}

